package com.itboye.lottery.activity.shuangseqiu

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.text.Html
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.itboye.lottery.R
import com.itboye.lottery.activity.TouZhuListActivity
import com.itboye.lottery.adapter.CommonPagerAdapter
import com.itboye.lottery.alert.ByAlert
import com.itboye.lottery.base.BaseActivity
import com.itboye.lottery.bean.CodeBean
import com.itboye.lottery.fragment.home.shuangseqiu.ShuangSeQiuFragment
import com.itboye.lottery.utils.BetSelectMatches.randomCodeTotal
import com.itboye.lottery.utils.CodeUtil.randomCommon
import com.itboye.lottery.utils.Combination
import kotlinx.android.synthetic.main.activity_shuangseqiu_main.*

class ShuangSeQiuMainActivity : BaseActivity() {


    internal var orderFragments: MutableList<Fragment>? = null
    var tv_jixuan: TextView? = null//机选
    var tv_bottom_clear: TextView? = null//清除已选
    var iv_bottom_increase: ImageView? = null//+
    var iv_bottom_reduce: ImageView? = null//-
    var tv_bottom_sure: TextView? = null//投注

    var randomRedCode: ArrayList<CodeBean> = ArrayList()//普通投注红色
    var randomBlueCode: ArrayList<CodeBean> = ArrayList()//普通投注蓝色

    var randomDanTuoRedCode: ArrayList<CodeBean> = ArrayList()//胆拖投注红色
    var randomDanTuoBlueCode: ArrayList<CodeBean> = ArrayList()//胆拖投注蓝色
    var randomRedTuoMaCode: ArrayList<CodeBean> = ArrayList()


    var selectionPosition: Int = 0

    override fun getLayoutId(): Int {
        return R.layout.activity_shuangseqiu_main
    }


    companion object {
        var activity: ShuangSeQiuMainActivity? = null
        var clear: Boolean = true
    }

    fun newinstance(): ShuangSeQiuMainActivity {
        return activity!!
    }

    override fun initData() {
        activity = this
        initTitle()
        bindTl_Vp()
    }

    private fun initTitle() {
        setTitle("双色球")
    }

    private var normalBet: ShuangSeQiuFragment? = null

    private var boldBet: ShuangSeQiuFragment? = null

    private fun bindTl_Vp() {
        orderFragments = ArrayList()
        normalBet = ShuangSeQiuFragment.newInstance(0)
        boldBet = ShuangSeQiuFragment.newInstance(1)
        orderFragments!!.add(normalBet!!)
        orderFragments!!.add(boldBet!!)
        vp_shuangseqiu.adapter = CommonPagerAdapter(this, orderFragments, R.array.shuangse_title, supportFragmentManager)
        vp_shuangseqiu.offscreenPageLimit = 5
        tb_shuangseqiu.setupWithViewPager(vp_shuangseqiu)
        vp_shuangseqiu.setCurrentItem(intent.getIntExtra("order_position", 0), false)
        vp_shuangseqiu.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                selectionPosition = position
            }

        })
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.tv_jixuan -> {
                //机选
                //生成随机号码选中：
                when (vp_shuangseqiu.currentItem) {
                    0 -> {
                        //生成红色号码
                        randomRedCode = randomCommon(1, 33, 6, 0)
                        //生成蓝色号码
                        randomBlueCode = randomCommon(1, 16, 1, 2, randomRedCode)

                        normalBet!!.redAdapter!!.randomCodeUpdate(randomRedCode)
                        normalBet!!.blueAdapter!!.randomCodeUpdate(randomBlueCode)
                    }
                    1 -> {
                        //生成红色(胆)号码
                        randomDanTuoRedCode = randomCommon(1, 33, 5, 0)
                        //生成红色脱码（不能与胆码相同）
                        randomRedTuoMaCode = randomCommon(1, 33, 2, 1, randomDanTuoRedCode)
                        //生成蓝色号码
                        randomDanTuoBlueCode = randomCommon(1, 16, 1, 2)

                        boldBet!!.redAdapter!!.randomCodeUpdate(randomDanTuoRedCode)
                        boldBet!!.redTuoMaAdapter!!.randomCodeUpdate(randomRedTuoMaCode)
                        boldBet!!.blueAdapter!!.randomCodeUpdate(randomDanTuoBlueCode)
                    }
                    else -> {
                    }
                }
                setCalculateZhuShu(caculateZhuShu(selectionPosition))
            }
            R.id.tv_bottom_clear -> {
                //清除已选
                boldBet!!.blueAdapter!!.clearAllStatus()
                boldBet!!.redAdapter!!.clearAllStatus()
                boldBet!!.redTuoMaAdapter?.clearAllStatus()

                normalBet!!.redAdapter!!.clearAllStatus()
                normalBet!!.blueAdapter!!.clearAllStatus()
            }

            R.id.tv_bottom_sure -> {
                //开始投注
                //遍历获取选中的号码(两个页面，如果有选，则要判断选中号码的个数贵则， 一个号码没选就视为不投注这种方式)
                if (isNormalBetTrue()) {
//                    randomCodeTotal.clear()
                    var map = HashMap<String, Any>()
                    var endTime = ""
                    when (selectionPosition) {
                        0 -> {
                            map["red"] = normalBet!!.redAdapter!!.selectCodeArray
                            map["blue"] = normalBet!!.blueAdapter!!.selectCodeArray
                            endTime = normalBet!!.tv_shuangseqiu_message_1!!.text.toString()
                        }
                        1 -> {
                            map["red"] = boldBet!!.redAdapter!!.selectCodeArray
                            map["blue"] = boldBet!!.blueAdapter!!.selectCodeArray
                            map["red_tuo"] = boldBet!!.redTuoMaAdapter!!.selectCodeArray
                            endTime = boldBet!!.tv_shuangseqiu_message_1!!.text.toString()
                        }
                    }
                    map["zhushu"] = num
                    randomCodeTotal.add(map)
                    if (TouZhuListActivity.activity != null) {
                        TouZhuListActivity.activity.finish()
                    }
                    startActivity(Intent(this, TouZhuListActivity::class.java)
                            .putExtra("arrayCode", randomCodeTotal)//所有号码
                            .putExtra("end_time", endTime)
                            .putExtra("type", true))
                }

            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        if (clear) {
            randomCodeTotal?.clear()
        }
    }

    private var danTuoBetSelect: Boolean = false
    private var normalBetSelect: Boolean = false

    private fun isNormalBetTrue(): Boolean {
        when (selectionPosition) {
            0 -> {
                if (normalBet!!.redAdapter!!.selectCodeArray.size < 6) {
                    ByAlert.alert("红球至少选择6个")
                    return false
                } else if (normalBet!!.blueAdapter!!.selectCodeArray.size < 1) {
                    ByAlert.alert("蓝球至少选择1个")
                    return false
                }
            }
            else -> {
                if (boldBet!!.redAdapter!!.selectCodeArray.size > 5 || boldBet!!.redAdapter!!.selectCodeArray.size <= 0) {
                    ByAlert.alert("红胆至少选择1个,最多选择六个")
                    return false
                } else if (boldBet!!.redTuoMaAdapter!!.selectCodeArray.size < 2) {
                    ByAlert.alert("红球拖码至少选择2个")
                    return false
                } else if (boldBet!!.blueAdapter!!.selectCodeArray.size < 1) {
                    ByAlert.alert("蓝球最少选择1个")
                    return false
                }
            }
        }
        //第一种 两个页面都没选中
//        if (normalBet!!.getSelcectCount()[0] +
//                normalBet!!.getSelcectCount()[2] +
//                boldBet!!.getSelcectCount()[0] +
//                boldBet!!.getSelcectCount()[1] +
//                boldBet!!.getSelcectCount()[2] == 0) {
//            ByAlert.alert("请先选择号码哦")
//            return false
//        }
//        if (normalBet!!.getSelcectCount()[0] != 0 || normalBet!!.getSelcectCount()[2] != 0) {
//            //红球至少6个 篮球至少一个
//            Log.v("code_count", "普通投注RED：" + normalBet!!.redAdapter!!.getSelectTypeCount(0))
//            if (normalBet!!.getSelcectCount()[0] < 6) {
//                ByAlert.alert("普通投注红球至少选择6个哦")
//                return false
//            }
//            Log.v("code_count", "普通投注Blue：" + normalBet!!.blueAdapter!!.getSelectTypeCount(2))
//            if (normalBet!!.getSelcectCount()[2] < 1) {
//                ByAlert.alert("普通投注蓝球至少选择1个哦")
//                return false
//            }
//            normalBetSelect = true
//        }
//
//        if (boldBet!!.getSelcectCount()[0] != 0 || boldBet!!.getSelcectCount()[1] != 0 || boldBet!!.getSelcectCount()[2] != 0) {
//            //红球至少6个 篮球至少一个
//            if (boldBet!!.getSelcectCount()[0] > 5 || boldBet!!.getSelcectCount()[0] <= 0) {
//                ByAlert.alert("胆拖投注红球最多选择5个，最少选择一个哦")
//                return false
//            }
//            Log.v("code_count", "胆拖投注RED_TUO：" + boldBet!!.redTuoMaAdapter!!.getSelectTypeCount(1))
//            if (boldBet!!.getSelcectCount()[1] < 2) {
//                ByAlert.alert("胆拖投注红球拖码至少选择2个哦")
//                return false
//            }
//            Log.v("code_count", "胆拖投注Blue：" + boldBet!!.blueAdapter!!.getSelectTypeCount(2))
//            if (boldBet!!.getSelcectCount()[2] < 1) {
//                ByAlert.alert("胆拖投注蓝球至少选择1个哦")
//                return false
//            }
//
//            danTuoBetSelect = true
//        }
        return true
    }


    fun caculateZhuShu(selectionPosition: Int): Int {
        //计算注数
        when (selectionPosition) {
//            0 -> {
//                var num = Combination.findsort(randomRedCode, 6).size
//                var num2 = Combination.findsort(randomBlueCode, 1).size
//                return num * num2
//            }
//            else -> {
//                var num = Combination.findsort(randomDanTuoRedCode, 5).size
//                var num3 = Combination.findsort(randomRedTuoMaCode, 6 - randomDanTuoRedCode.size).size
//                var num4 = Combination.findsort(randomDanTuoBlueCode, 1).size
//                return num * num3 * num4
//            }
            0 -> {
                var num = Combination.findsort(randomRedCode, 6)
                var num2 = Combination.findsort(randomBlueCode, 1)
                return num * num2
            }
            else -> {
                var num = Combination.findsort(randomDanTuoRedCode, 5)
                var num3 = Combination.findsort(randomRedTuoMaCode, 6 - randomDanTuoRedCode.size)
                var num4 = Combination.findsort(randomDanTuoBlueCode, 1)
                return num * num3 * num4
            }
        }

    }

    var num = 0
    fun setCalculateZhuShu(num: Int) {
        this.num = num
        tv_bottom_heji!!.text = Html.fromHtml("<font color='red'>${num * 2}元</font><br/>${num}注")
    }


}
